package com.fa4j.system.notice.listener;

import com.fa4j.system.account.model.AccountPasswordRestEvent;
import com.fa4j.system.account.model.AccountPasswordType;
import com.fa4j.system.notice.model.NoticeCreateCmd;
import com.fa4j.system.notice.service.NoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Async
@Slf4j
@Component
@RequiredArgsConstructor
public class NoticeEventListener {

    private final NoticeService noticeService;

    @EventListener
    public void handlePwdReset(AccountPasswordRestEvent event) {
        if (event.getType() == AccountPasswordType.LOGIN) {
            String message = "您的登录密码已被重置,临时密码为【%s】,有效期为%s分钟,请尽快修改"
                    .formatted(event.getPassword(), event.getExpireMinutes());
            NoticeCreateCmd cmd = new NoticeCreateCmd();
            cmd.setAccountId(event.getAccountId());
            cmd.setTitle("密码被重置");
            cmd.setContent(message);
            cmd.setLink("/home/userPwdModify");
            cmd.setOperatorId(event.getOperatorId());
            noticeService.create(cmd);
        }
    }
}
